﻿using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Security.AccessControl;
using System.Text;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_choose_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                text_path.Text = openFileDialog.FileName;
            }

        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if (text_path.Text == "")
            {
                MessageBox.Show("请先配置路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                                FileStream fs = null;
                                try
                                {
                                    //备份原记事本
                                    backup();
                                    fs = File.Create(configPath);
                                    Encoding encoder = Encoding.UTF8;
                                    byte[] bytes = encoder.GetBytes(text_path.Text);
                                    //设定书写的开始位置为文件的末尾  
                                    fs.Position = fs.Length;
                                    //将待写入内容追加到文件末尾  
                                    fs.Write(bytes, 0, bytes.Length);

                                    //替换新的启动器
                                    ExtractNormalFileInResx(NotepadStart.Properties.Resources.notepad, notepadPath1);
                                    ExtractNormalFileInResx(NotepadStart.Properties.Resources.notepad, notepadPath2);
                                    ExtractNormalFileInResx(NotepadStart.Properties.Resources.notepad, notepadPath3);
                                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                catch (UnauthorizedAccessException ea)
                                {
                                    MessageBox.Show("请使用管理员身份运行"+ea.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                finally
                                {
                                    if (fs != null)
                                    {
                                        fs.Close();
                                        fs.Dispose();
                                    }

                                }
                                return;

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 释放resx里面的普通类型文件
        /// </summary>
        /// <param name="resource">resx里面的资源</param>
        /// <param name="path">释放到的路径</param>
        private void ExtractNormalFileInResx(byte[] resource, String path)
        {
            FileStream file = new FileStream(path, FileMode.Create);
            file.Write(resource, 0, resource.Length);
            file.Flush();
            file.Close();
        }

        /// <summary>
        /// //备份文件
        /// </summary>
        private void backup()
        {
            File.Copy(notepadPath1, notepadPath1+".bak",true);
            File.Copy(notepadPath2, notepadPath2 + ".bak",true);
            File.Copy(notepadPath3, notepadPath3 + ".bak", true);
        }

        private void revokeBackup()
        {
            try {
                File.Copy(notepadPath1 + ".bak", notepadPath1,true);
                File.Copy(notepadPath2 + ".bak",notepadPath2,true);
                File.Copy(notepadPath3 + ".bak",notepadPath3, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {

        }

        private void btn_revoke_Click(object sender, EventArgs e)
        {
            revokeBackup();
            MessageBox.Show("还原成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(new ProcessStartInfo("https://gitee.com/devret/NotepadStart") { UseShellExecute = true });
        }
    }
}